const express = require('express');
const mongoose = require('mongoose');
//链接mongoDB  并且使用react-study这个集合
const DB_URL = 'mongodb://localhost:27017/react-study';
mongoose.connect(DB_URL);
mongoose.connection.on('connected', () => {
    console.log('mongo connect success');
});

const User = mongoose.model('user', mongoose.Schema({
    user: {type: String, require: true},
    age: {type: Number, require: true},
}))
// User.create({
//     user: 'imooc',
//     age: 18
// }, (err, doc) => {
//     if (!err) {
//         console.log(doc);
//     } else {
//         console.log(err);
//     }
// })
const app = express();

app.get('/', (req, res) => {
    res.send('<h1>Hello world</h1>');
});

app.get('/find', (req, res) => {
    User.find({}, (err, doc) => {
        if (!err) {
            res.json(doc);
        } else {
            res.json(err);
        }
    })
})

app.listen(9093, function() {
    console.log('express app start at port 9093');
})